package com.zjw.yyyy2021.mmdd0922;

import java.util.Arrays;

/**
 *
 *
 * @author 郑大仙丶
 * @version V1.0.0
 * @date 2021/9/22 20:26
 */
public class MMdd0922 {

    public static ListNode[] splitListToParts(ListNode head, int k) {
        ListNode[] result = new ListNode[k];
        if (head == null) {
            return result;
        }
        int length = 1;
        ListNode current = head;
        while (current.next != null) {
            current = current.next;
            length++;
        }
        // 5
        // 1    0
        int step = length / k;
        int more = length % k;
        for (int i = 0; i < k && head != null; i++) {
            result[i] = head;
            int n = step + (i < more ? 1 : 0);
            for(int j = 1;j < n;j++){
                head = head.next;
            }
            ListNode temp = head.next;
            head.next = null;
            head = temp;
        }
        return result;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode();
        ListNode tail1 = new ListNode();
        ListNode tail2 = new ListNode();
        head.val = 1;
        head.next = tail1;
        tail1.val = 2;
        tail1.next = tail2;
        tail2.val = 3;
        ListNode[] listNodes = splitListToParts(head, 2);
        System.out.println(Arrays.toString(listNodes));
    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                '}';
    }
}
